package com.example.score.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.score.entity.ClassInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface ClassInfoMapper extends BaseMapper<ClassInfo> {

    @Select("SELECT * FROM t_class_info WHERE exam_detail_id = #{examDetailId} ORDER BY class_name")
    List<ClassInfo> selectByExamDetailId(@Param("examDetailId") Long examDetailId);

    @Select("SELECT * FROM t_class_info WHERE exam_detail_id = #{examDetailId} AND class_id = #{classId}")
    ClassInfo selectByExamDetailIdAndClassId(@Param("examDetailId") Long examDetailId, @Param("classId") Long classId);


    @Select("SELECT grade_info_id FROM t_class_info WHERE class_id = #{classId} and exam_detail_id =#{examId}")
    Long selectGradeInfoIdByClassId(@Param("classId") Long classId, @Param("examId") Long examId);


    @Select("SELECT ci.*, COUNT(ss.id) as student_submit_count " +
            "FROM t_class_info ci " +
            "LEFT JOIN t_student_submit ss ON ci.id = ss.class_info_id " +
            "WHERE ci.exam_detail_id = #{examDetailId} and ss.class_id = #{classId} " +
            "GROUP BY ci.id")
    ClassInfo selectByExamDetailIdAndClassIdId(@Param("examDetailId") Long examDetailId,
                                                     @Param("classId") Long classId);

    @Insert("<script>" +
            "INSERT INTO t_class_info (exam_detail_id, grade_info_id, class_id, class_name, student_count, student_submit_count, status, created_time, updated_time, class_firstpushscore_time, class_oms_id) VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.examDetailId}, #{item.gradeInfoId}, #{item.classId}, #{item.className}, #{item.studentCount}, #{item.studentSubmitCount}, #{item.status}, now(), now(), #{item.classFirstpushscoreTime}, #{item.classOmsId})" +
            "</foreach>" +
            "</script>")
    void insertBatch(@Param("list") List<ClassInfo> classInfos);

    @Select("SELECT * FROM t_class_info WHERE status = 0")
    List<ClassInfo> selectUnsyncedClasses();

    @Select("SELECT * FROM t_class_info ci " +
            "WHERE ci.exam_detail_id = #{examDetailId} " +
            "AND ci.grade_info_id = #{gradeId}")
    List<ClassInfo> selectByExamDetailIdAndGradeId(@Param("examDetailId") Long examDetailId,
                                                   @Param("gradeId") Long gradeId);

    @Select("SELECT * FROM t_class_info WHERE class_id = #{classId} and exam_detail_id =#{examDetailId}  limit 1")
    ClassInfo selectByClassId(@Param("classId") Long classId, @Param("examDetailId") Long id);

    @Select("SELECT * FROM t_class_info WHERE class_id = #{classId}  limit 1")
    ClassInfo selectByClassId2(@Param("classId") Long classId);
    /**
     * 根据年级ID查询班级列表
     */
    @Select("SELECT * FROM t_class_info WHERE grade_info_id = #{gradeId}")
    List<ClassInfo> selectByGradeId(@Param("gradeId") Long gradeId);

    @Select("<script>SELECT * FROM t_class_info WHERE grade_info_id in " +
            "<foreach collection='gradeIds' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<ClassInfo> selectByGradeIds(@Param("gradeIds") List<Long> gradeIds);

    @Select("<script>SELECT class_id as classId FROM t_class_info WHERE grade_info_id in " +
            "<foreach collection='gradeIds' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Long> selectClassIdsByGradeIds(@Param("gradeIds") List<Long> gradeIds);

    @Select("<script>SELECT exam_detail_id as examDetailId, class_id as classId, class_name as className, student_count as studentCount, student_submit_count as studentSubmitCount  FROM t_class_info WHERE class_id in " +
            "<foreach collection='classIds' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<ClassInfo> selectByClassIds(List<Long> classIds);

    @Select("<script>SELECT exam_detail_id as examDetailId, class_id as classId, class_name as className, student_count as studentCount, student_submit_count as studentSubmitCount  FROM t_class_info WHERE class_id in " +
            "<foreach collection='classIds' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach> and exam_detail_id in <foreach collection='examIds' item='examId' open='(' separator=',' close=')'>" +
            "#{examId}" +
            "</foreach>" +
            "</script>")
    List<ClassInfo> selectByClassIdsAndExamIds(@Param("classIds") List<Long> classIds, @Param("examIds") List<Long> examIds);
}